import { ApiDataContext } from './../domain/apiDataContext';
import { IQueryObject } from 'tiny-entity2/queryObject';
import { BaseService } from "./baseService";
import { Transaction } from "tiny-entity2";
import { Component } from "../domain/models/componet";
import { DataSource } from '../domain/models/dataSource';

export class ComponentService extends BaseService {
    GetTmpComItemById(id: string) {
        let ctx = new ApiDataContext();
        return ctx.TmpComItem.First(x => x.id == $args1, { $args1: id });
    }

    CreateComponet(entity: Component, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Component();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    UpdateComponet(entity: Component, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new Component();
            d.ConverToEntity(entity);
            return ctx.Create(d);
        });
    }
    GetComponetById(id: string) {
        let ctx = new ApiDataContext();
    }
    QueryComponet(params) {
        let ctx = new ApiDataContext();
        let q = ctx.Component as IQueryObject<Component>;

        return q.ToList();
    }

    async InitDefaultComponent() {
        let ctx = new ApiDataContext();
        await Transaction(ctx, async ctx => {
            for (let item of componentConfigs) {
                let obj = new Component();
                obj.ConverToEntity(item);
                let has = await ctx.Component.First(x => x.id == $args1, { $args1: item.id });
                if (has) {
                    await ctx.Update(obj);
                }
                else {
                    await ctx.Create(obj);
                }
            }
        });

    }

    async InitDataSource() {
        let ctx = new ApiDataContext();
        await Transaction(ctx, async ctx => {
            for (let item of dsConfigs) {
                let ds = new DataSource();
                ds.ConverToEntity(item);
                let has = await ctx.DataSource.First(x => x.id == $args1, { $args1: ds.id });
                if (has) {
                    await ctx.Update(ds);
                }
                else {
                    await ctx.Create(ds);
                }
            }
        });

    }
}

const componentConfigs = [
    {
        id: '11f111f508a847488ee91e920d02db39',
        selector: 'input-text',
        labelText: '文本输入',
        name: '文本输入',
        type: 'base'
    },
    {
        id: '1cd6b4224e944ffbbc5bcb25ccfe1934',
        selector: 'rmb-capitalize',
        labelText: '货币转大写',
        name: '货币转大写',
        type: 'base'
    },
    {
        id: '0ed6c2124da1475297c676dcc5666afe',
        selector: 'project-number',
        labelText: '项目编号生成',
        name: '项目编号生成',
        type: 'base'
    },
    {
        id: 'f01849198e36456f9d13cded3def816b',
        selector: 'chart-loop',
        labelText: '环比图',
        name: '环比图',
        type: 'base'
    },
    {
        id: '65ab4237f24f4911bd4c1f9aeb74ce62',
        selector: 'chart-strip',
        labelText: '条形图',
        name: '条形图',
        type: 'base'
    },
    {
        id: '3660b24000394b66852d7bccd895bc08',
        selector: 'chart-radar',
        labelText: '雷达图',
        name: '雷达图',
        type: 'base'
    },
    {
        id: '8e24f57ea096411899e7054f02e46f85',
        selector: 'chart-pie',
        labelText: '饼图',
        name: '饼图',
        type: 'base'
    },
    {
        id: 'e506e7d6f995406589e3542d711d30c3',
        selector: 'single-date',
        labelText: '单日期',
        name: '单日期',
        type: 'base'
    },
    {
        id: '259b81f95a7045a7a8773aff18862825',
        selector: 'start-end-date',
        labelText: '起止日期',
        name: '起止日期',
        type: 'base'
    },
    {
        id: '40f2588c60004084b6de5a38362e5b6c',
        selector: 'files-upload',
        labelText: '文件上传',
        name: '文件上传',
        type: 'base'
    },
    {
        id: '24ad2bbcd6f7411f8cbd75d7589ec06e',
        selector: 'base-table',
        labelText: '表格',
        name: '表格',
        type: 'base'
    },
    {
        id: 'df31126f16d74f5599fbe0e240f8bf7c',
        selector: 'information',
        labelText: '消息通知',
        name: '消息通知',
        type: 'base'
    },
    {
        id: '97e6ad9c02804b0ea666f075d9547b15',
        selector: 'memo',
        labelText: '备忘录',
        name: '备忘录',
        type: 'base'
    },
    {
        id: '0c01827a2aa145d3b4e57ef20f54af48',
        selector: 'tabs',
        labelText: '标签页',
        name: '标签页',
        type: 'base'
    },
    // {
    //     id: '54c654efe7af40e98f240ef5fac216fc',
    //     selector: 'text-area',
    //     labelText: '富文本',
    //     name: '富文本',
    //     type: 'base'
    // },
    {
        id: 'ace494c9745a4654992a3d17cdd47816',
        selector: 'search-bar',
        labelText: '搜索工具条',
        name: '搜索工具条',
        type: 'base'
    },
    {
        id: '187d8dc932be45e2919f06195dde7fc0',
        selector: 'select',
        labelText: '下拉框',
        name: '下拉框',
        type: 'base'
    },
    {
        id: 'e9ce497f2ce0470db66974d9f52d8dc8',
        selector: 'address-select',
        labelText: '行政区域',
        name: '行政区域',
        type: 'base'
    },
    {
        id: '46cb21f84a2246ee950e30cb3ad2e722',
        selector: 'audit',
        labelText: '审核',
        name: '审核',
        type: 'base'
    },
    {
        id: '585895f2c20c407b8a0b53ae66770285',
        selector: 'space-occupying',
        labelText: '占位文本',
        name: '占位文本',
        type: 'base'
    },
    {
        id: 'f58818d1443e4f6290fa2cbc045bfdb2',
        selector: 'auto-form',
        labelText: '用户自动表单',
        name: '用户自动表单',
        type: 'base'
    },
    {
        id: 'cbe961ee620d4ee89e4a072f7f9d9336',
        selector: 'column-table',
        labelText: '单元表格',
        name: '单元表格',
        type: 'base'
    },
    {
        id: 'c78e84430bc840939a5c994ebe8c0184',
        selector: 'file-maker',
        labelText: '文件制作',
        name: '文件制作',
        type: 'base'
    },
    {
        id: 'ca20df3a11eb4632b03fa530dea7344e',
        selector: 'button-group',
        labelText: '按钮组',
        name: '按钮组',
        type: 'base'
    },
    {
        id: 'ca495c9569f64b0eb9a19a3f5cb14837',
        selector: 'bid-opening',
        labelText: '开标演示',
        name: '开标演示',
        type: 'base'
    },
    {
        id: 'f5df05d5bde644048af86f724aeade70',
        selector: 'dividing-line',
        labelText: '分割线',
        name: '分割线',
        type: 'base'
    },
    {
        id: '50cf944cfdd34f408e20a8b49030c09f',
        selector: 'bid-opening-ceremony',
        labelText: '开标仪式',
        name: '开标仪式',
        type: 'base'
    },
    {
        id: '57e11fc8948e4cfc968540cc318ac175',
        selector: 'doc-assembly',
        labelText: '文档汇编生成',
        name: '文档汇编生成',
        type: 'base'
    },
    {
        id: '4d10a98ce99c40fd88932dd1be628d2f',
        selector: 'task-member',
        labelText: '任务成员',
        name: '任务成员',
        type: 'base'
    },
    {
        id: '910b38776ee54718a68be18ddb98230b',
        selector: 'bid-evaluation',
        labelText: '开标评标',
        name: '开标评标',
        type: 'base'
    },
    {
        id: '218242626fb74a079c9e7f098a864e6a',
        selector: 'early-warning',
        labelText: '预警消息',
        name: '预警消息',
        type: 'base'
    },
    {
        id: 'd240015f7ec949eabcfb2be22a127e30',
        selector: 'short-message',
        labelText: '短信通知',
        name: '短信通知',
        type: 'base'
    },
    {
        id: '6f2ac1f029bc4c96808fae3fe8214ded',
        selector: 'place-select',
        labelText: '场地选择',
        name: '场地选择',
        type: 'base'
    },
    {
        id: '108dda1db2144c3aa9e9fb6072d707fb',
        selector: 'task-top-bar',
        labelText: '项目任务栏',
        name: '项目任务栏',
        type: 'base'
    },
    {
        id: 'e8586702d6f445909e6bf94dba457513',
        selector: 'calendar-memo',
        labelText: '日历备忘录',
        name: '日历备忘录',
        type: 'base'
    },
    {
        id: '990c7f66f17646efa10549a726df2a37',
        selector: 'task-notification',
        labelText: '任务通知',
        name: '任务通知',
        type: 'base'
    },
    {
        id: 'c87770efe89240c68be2194c7604adf8',
        selector: 'address-book',
        labelText: '通讯录',
        name: '通讯录',
        type: 'base'
    },
    {
        id: '1bccc1650bae42dfb95803fc4a36a743',
        selector: 'office-tool',
        labelText: '智能OA',
        name: '智能OA',
        type: 'base'
    },
    {
        id: '29e33f4c0eb544558395b5834ea69303',
        selector: 'sys-sync',
        labelText: '供应商系统同步',
        name: '供应商系统同步',
        type: 'base'
    },
    {
        id: 'b2103484176e4cac86f5d8c687f1b575',
        selector: 'wf-create',
        labelText: '流程创建',
        name: '流程创建',
        type: 'base'
    },
    {
        id: 'c232b484176e4cac86f5d8c6y7f1b2u2',
        selector: 'sys-result-sync',
        labelText: '供应商系统结果同步',
        name: '供应商系统结果同步',
        type: 'base'
    }
]

const dsConfigs = [
    {
        id: '5f400959b9954a2bb93a116395c13515',
        name: '组织机构.部门',
        dataType: "department",
        orgId: 'global',
        data: [
            { bindField: "id", name: "部门id" },
            { bindField: "name", name: "部门名称" }
        ]
    },
    {
        id: 'a7c83f036adb41d7b79798d22dec7bd3',
        name: '组织机构.员工',
        orgId: 'global',
        dataType: "user",
        data: [
            { bindField: "id", name: "员工id" },
            { bindField: "name", name: "员工姓名" },
            { bindField: "employeeNo", name: "工号" },
            { bindField: "gender", name: "性别" },
            { bindField: "phone", name: "电话" },
            { bindField: "position", name: "职位" },
            { bindField: "idCard", name: "身份号" },
            { bindField: "email", name: "电子邮箱" },
            { bindField: "departmentNode.nodeName", name: "部门名称" },
            { bindField: "telephone", name: "座机号码" }
        ]
    },
    {
        id: '65d76d0f4a1048a59a79369bb5d8b2cf',
        name: '组织机构.单位',
        orgId: 'global',
        dataType: "organization",
        data: [
            { bindField: "id", name: "单位id" },
            { bindField: "name", name: "单位名称" }
        ]
    },
    {
        id: '87811937a3824df18c4c6af0fa464bfb',
        name: '更正记录',
        orgId: 'global',
        dataType: "dataChangeLog",
        data: [
            { bindField: "id", name: "id" },
            { bindField: "createTime", name: "创建时间" },
            { bindField: "updateTime", name: "修改时间" },
            { bindField: "status", name: "状态" },
            { bindField: "operatorId", name: "操作人id" },
            { bindField: "operatorName", name: "操作人名称" },
        ]
    },
    {
        id: 'a4e76d8daa8f40ce9be9e9f3480b8866',
        name: '消息列表数据源',
        orgId: 'global',
        dataType: "message",
        data: [
            { bindField: "id", name: "id" },
            { bindField: "name", name: "消息名称" },
            { bindField: "title", name: "标题" },
            { bindField: "body", name: "内容" },
            { bindField: "createTime", name: "创建时间" },
            { bindField: "updateTime", name: "更新时间" },
            { bindField: "status", name: "状态" },
            { bindField: "type", name: "消息类型" },
            { bindField: "sender", name: "发送人" }
        ]
    }
]